﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DisplayBoardInfo.Client.Forms
{
    public delegate void AddedDelegate(object oSender, EventArgs oEventArgs);

    public partial class AddPatient : Form
    {
        private Boolean closed;

        public event AddedDelegate AddingDone;

        public void Add(string sFirstName, string sLastName, DateTime dAppointmentTime, int sVisitTime, string sPesel)
        {
            ChangingArgs oAddingArgs = new ChangingArgs(sFirstName, sLastName, dAppointmentTime, sVisitTime, sPesel);
            FireAddingEvent(oAddingArgs);
        }

        public void FireAddingEvent(EventArgs oEventArgs)
        {
            if (null != AddingDone)
            {
                AddingDone(this, oEventArgs);
            }
        }

        public AddPatient(DateTime date)
        {
            InitializeComponent();
            appointmentDTP.Format = DateTimePickerFormat.Custom;
            appointmentDTP.CustomFormat = "HH:mm";
            appointmentDTP.ShowUpDown = true;
            appointmentDTP.Value = date;
            closed = false;

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            closed = true;
            this.Close();
        }

        private void addPatient_FormClosed(object sender, FormClosedEventArgs e)
        {
            closed = true;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (firstNameTB.Text != "" && lastNameTB.Text != "" &&
                appointmentDTP != null && visitTimeUD.Value != null && peselTB.Text != "")
            {
                if (peselTB.Text.Count() == 11)
                {
                    Add(firstNameTB.Text, lastNameTB.Text, appointmentDTP.Value, (Int32)visitTimeUD.Value, peselTB.Text);
                    this.closed = true;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("PESEL powinien się składać z 11 cyfr.", "Błąd dodawania pacjenta",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                MessageBox.Show("Wszystkie pola są wymagane.", "Błąd dodawania pacjenta",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
            }
        }

        public Boolean isClosed
        {
            get { return this.closed; }
        }
    }
}
